package com.iursp.web;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.iursp.conf.annotation.CacheLock;
import com.iursp.conf.annotation.CacheParam;
import com.iursp.domain.model.user.User;
import com.iursp.domain.result.ExceptionMsg;
import com.iursp.domain.result.Response;
import com.iursp.service.user.UserService;

import io.swagger.annotations.ApiOperation;


@RestController 
@RequestMapping(value="/auth/users") 
public class UserController extends BaseController{
	
	@Autowired
	private UserService userService;
	
	// 创建线程安全的Map 
	static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>()); 
	
	@ApiOperation(value="获取用户列表", notes="")
	@GetMapping(value="/")
	public PageInfo<User> getUserList(@RequestParam String limit,@RequestParam String offset) { 
		return userService.queryAllUsers(new User());
	} 
	
	@ApiOperation(value="创建用户", notes="根据User对象创建用户")
	@CacheLock(prefix = "users")
	@RequestMapping(value="/",method=RequestMethod.POST) 
	public String postUser(@CacheParam(name = "token") @RequestParam String token,@ModelAttribute User user) { 
		return "success - " + token;
	} 
	
	@ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
	@RequestMapping(value="/{id}", method=RequestMethod.GET) 
	public User getUser(@PathVariable Long id) { 
		// 处理"/users/{id}"的GET请求，用来获取url中id值的User信息 
		// url中的id可通过@PathVariable绑定到函数的参数中 
		return users.get(id); 
	} 
	
	@ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象，并根据传过来的user信息来更新用户详细信息")
	@RequestMapping(value="/{id}", method=RequestMethod.PUT) 
	public String putUser(@PathVariable Long id, @ModelAttribute User user) { 
		// 处理"/users/{id}"的PUT请求，用来更新User信息 
		User u = users.get(id); 
		//u.setName(user.getName()); 
		//u.setAge(user.getAge()); 
		users.put(id, u); 
		System.out.println("2");
		return "success"; 
	} 
	
	@ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
	@RequestMapping(value="/{id}", method=RequestMethod.DELETE) 
	@CacheLock(prefix = "users")
	public Response deleteUser(@PathVariable String id,@CacheParam(name = "token") @RequestParam(value="token",required=false) String token) { 
		if(null  != id && StringUtils.isNotBlank(id)){
			try {
				int status = userService.delUserById(id);
				return new Response("0000",status == 1 ? "删除用户成功":"删除用户失败");
			} catch (Exception e) {
				return result(ExceptionMsg.FAILED);
			}
		}else{
			logger.error("删除用户id为空，请检查参数");
			return result(ExceptionMsg.ParamError);
		}
	} 
	

}
